package com.tangding.hprose.api.server.command.impl.log;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tangding.common.exception.GException;
import com.tangding.common.utils.DateUtils;
import com.tangding.hprose.api.server.command.Command;
import com.tangding.hprose.api.server.command.Response;
import com.tangding.service.dao.log.LogRepository;

/**
 * @description 
 *
 * @author Ranger
 * @version 2017年12月20日_下午2:27:57
 *
 */
@Component
public class QueryLogs implements Command {

	@Autowired
	LogRepository logRepository;
	
	@Override
	public Response exe(Object... params) throws GException {
		String uid = params[0].toString();
		Document condition = new Document();
		condition.put("uid", uid);
		List<Document> list = logRepository.findAll(condition);
		List<Map<String, Object>> response = new ArrayList<>();
		list.forEach(doc -> {
			Date createTime = doc.getDate("time");
			String cmdDisplay = doc.getString("cmdDisplay");
			String result = doc.getString("responseDisplay");
			Map<String, Object> map = new HashMap<>();
			map.put("time", DateUtils.formatDate(createTime, DateUtils.DATE_TIME_FORMAT));
			map.put("cmd", cmdDisplay);
			map.put("result", result);
			response.add(map);
		});
		
		return doResponseSucc(response);
	}

}
